package handler

import (
	"Readme/inits"
	"Readme/jwt/jwt"
	"Readme/jwt/upload"
	"Readme/request"
	"Readme/ted"
	"context"
	"github.com/gin-gonic/gin"
	"log"
	"math/rand"
	"net/http"
	"time"
)

func Login(c *gin.Context) {
	var data request.UserMysql
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	ints, _ := inits.Rdb.Get(context.Background(), "incr:"+data.Mobile).Int()
	if ints >= 5 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证码错误5次,请重新发送验证码",
			"data": nil,
		})
		inits.Rdb.Del(context.Background(), "send:"+data.Mobile)
		inits.Rdb.Del(context.Background(), "incr:"+data.Mobile)

		return
	}
	cod, _ := inits.Rdb.Get(context.Background(), "send:"+data.Mobile).Int()
	if cod != data.Code {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证码错误",
			"data": nil,
		})
		inits.Rdb.Incr(context.Background(), "incr:"+data.Mobile)
		return
	}
	mysql, err := ted.LoginMysql(data.Mobile, data.Password)
	if mysql.Id == 0 {
		register, _ := ted.Register(data.Mobile, data.Password)
		if register.Id == 0 {
			c.JSON(http.StatusOK, gin.H{
				"code": 10000,
				"msg":  "注册失败",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "注册成功",
			"data": nil,
		})
	}
	claims := jwt.CustomClaims{
		ID: uint(mysql.Id),
	}
	token, err := jwt.NewJWT("2209A").CreateToken(claims)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "token创建失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "登陆成功",
		"data": token,
	})
}

func SendSms(c *gin.Context) {
	var data request.Montrd
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	code := rand.Intn(9000) + 1000
	//_, err = send.SendSms(data.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 10000,
	//		"msg":  "验证码发送失败",
	//		"data": nil,
	//	})
	//	return
	//}
	inits.Rdb.Set(context.Background(), "send:"+data.Mobile, code, time.Minute*20)
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "D:\\gocode\\src\\gao3\\rk\\2\\19\\yuekao\\Readme\\modric\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	upload.Upload(dst)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "",
		"data": nil,
	})
}

func Cont(c *gin.Context) {
	var data request.Conts
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	cont, err := ted.Conts(int(userId), data.Name, data.Neiroh)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "ok",
		"data": cont.Id,
	})
}

func Dianzan(c *gin.Context) {
	dianzan, err := ted.Dianzan()
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "ok",
		"data": dianzan,
	})
}

func Guanzhu(c *gin.Context) {
	var data request.Guznzhu
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	dianzan, err := ted.Guanzhu(userId, data.UserId)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "关注成功",
		"data": dianzan,
	})
}

func DianZan1(c *gin.Context) {
	var data request.Guznzhu
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	dianzan, err := ted.Guanzhu(userId, data.UserId)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "评论点赞成功",
		"data": dianzan,
	})
}

func Pinglun(c *gin.Context) {
	var data request.PingLun
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")

	lun, err := ted.PingLun(userId, data.PinlunId, data.FandId, data.Content)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "评论成功",
		"data": lun,
	})
}
